<template>
	<view class="content">
		<view class="block swiper-block">
			<SwiperMap style="width: 100%;height: 100%;border-radius: 20rpx;"
				:list="[{id:'1',imgUrl:imageUrl+'/train/banner.jpg'}]"></SwiperMap>
		</view>
		<view class="menu-item">
			<view class="tab-item" :class="tabActive==0?'active':''" @click="bindActive(0)">培训报名</view>
			<!-- <view class="tab-item" :class="tabActive==1?'active':''" @click="bindActive(1)">视频课程</view> -->
			<view class="tab-item" :class="tabActive==2?'active':''" @click="bindActive(2)">培训计划</view>
		</view>
		<view class="block apply-block" v-if="tabActive==0">
			<!-- <view class="title">
				<view class="t-left">
					<view class="pre-icon"></view>
					<view class="name">培训报名</view>
				</view>
				<view @click="navigate('/page_other/train/train_list')" class="more">全部</view>
			</view> -->
			<block v-if="applyList.length>0">
				<view class="item-block" v-for="(item) in applyList" :key="item.id"
					@click="navigate('/page_other/train/detail?id='+item.id+'&type='+item.trainState)">
					<view class="top-title">
						<view class="apply-title">{{item.company}}</view>
						<!-- <view class="btn" v-if="item.trainState===1">>立即报名</view>
						<view class="btn" v-else>已开班</view> -->
					</view>
					<view class="desc-block">
						<!-- <view class="desc">
							<image :src="imageUrl+'/train/department_icon.png'" mode="widthFix" class="desc-icon">
							</image>
							<view class="desc-text"><text class="label">机构：</text>{{item.company}}</view>
						</view> -->
						<view class="desc">
							<view class="desc-icon iconfont icon-dianhua"></view>
							<!-- <image :src="imageUrl+'/train/time_icon.png'" mode="widthFix" class="desc-icon"></image> -->
							<view class="desc-text" v-if="item.mobile"><text class="label">电话：</text>{{item.mobile}}
							</view>
							<view class="desc-text" v-else><text class="label">电话：</text>暂无</view>
						</view>
						<view class="desc">
							<image :src="imageUrl+'/train/addr_icon.png'" mode="widthFix" class="desc-icon"></image>
							<view class="desc-text"><text class="label">地址：</text>{{item.transaddress}}</view>
						</view>
					</view>
				</view>
			</block>
			<view v-else-if="applyList.length==listConf.count&&!listConf.more" style="text-align: center;">没有更多了哦</view>
			<view v-else class="empty">暂无数据</view>
		</view>
		<view class="block video-block" v-if="tabActive==1">
			<!-- <view class="title">
				<view class="t-left">
					<view class="pre-icon"></view>
					<view class="name">视频课程</view>
				</view>
				<view @click="navigate('/page_other/train/course')" class="more">全部</view>
			</view> -->
			<view v-if="videoList.length>0" class="video-item">
				<view class="video" v-for="(item) in videoList" :key="item.id">
					<video @play="play('video_'+item.id)" :id="'video_'+item.id" :src="item.vurl" :title="item.vname"
						class="video-icon"></video>
					<view class="name">{{item.vname}}</view>
				</view>
			</view>
			<view v-else-if="videoList.length>0&&!listConf.more" style="text-align: center;">没有更多了哦</view>
			<view v-else class="empty">暂无数据</view>
		</view>
		<view class="block news-block" v-if="tabActive==2">
			<!-- <view class="title">
				<view class="t-left">
					<view class="pre-icon"></view>
					<view class="name">政策补贴</view>
				</view>
				<view @click="navigate('/page_other/train/police')" class="more">全部</view>
			</view> -->
			<block v-if="newsList.length>0">
				<view class="news-item" v-for="(item) in newsList" :key="item.id"
					@click="navigate('/page_other/article/detail?id='+item.id)">
					<view class="news">
						<image :src="item.ima_url" mode="aspectFill" class="news-icon"></image>
						<view class="news-content">
							<view class="news-title">{{item.title}}</view>
							<view class="desc">{{item.abstract}}</view>
							<view class="time">发布时间：{{item.crt_time}}</view>
						</view>
					</view>
				</view>
			</block>
			<view v-else-if="newsList.length>0&&!listConf.more" style="text-align: center;">没有更多了哦</view>
			<view v-else class="empty">暂无数据</view>
		</view>

	</view>
</template>

<script>
	import {
		getTrainingList,
		getVideoTrainList
	} from '@/api/train.js';
	import {
		getNewsList as getNewsListApi
	} from '@/api/notice.js'
	import {
		isMore
	} from '@/util/util.js'
	export default {
		data() {
			return {
				tabActive: 0,
				applyList: [],
				newsList: [],
				videoList: [],
				imageUrl: '',
				videoContext: null,
				videoId: '',
				listConf: {
					page: 1,
					limit: 999,
					more: true,
					count: 0
				},
			}
		},
		methods: {
			bindActive(index) {
				this.tabActive = index
				if (this.tabActive == 0) {
					this.getTrainList(true)
				}
				if (this.tabActive == 1) {
					this.getVideoList(true);
				}
				if (this.tabActive == 2) {
					this.getNewsList(true);
				}
			},
			getTrainList(refresh = false) {
				getTrainingList({
					page: this.listConf.page,
					limit: this.listConf.limit,
				}).then((res) => {
					let list = res.modelList || [];
					this.applyList = refresh ? list : [...this.applyList, ...list];
					this.listConf.count = res.count
					this.listConf.more = isMore(this.listConf.page, this.listConf.limit, res.count);
				})
			},
			getNewsList(refresh = false) {
				getNewsListApi({
					type: 1,
					page: this.listConf.page,
					limit: this.listConf.limit,
				}).then((res) => {
					let list = res.list || [];
					console.log(list)
					this.newsList = refresh ? list : [...this.newsList, ...list];
					this.listConf.count = res.count
					this.listConf.more = isMore(this.listConf.page, this.listConf.limit, res.count);
				})
			},
			getVideoList(refresh = false) {
				getVideoTrainList({
					page: this.listConf.page,
					limit: this.listConf.limit,
					type: 0
				}).then((res) => {
					console.log(res)
					let list = res.modelList || [];
					this.videoList = refresh ? list : [...this.videoList, ...list];
					this.listConf.count = res.count
					this.listConf.more = isMore(this.listConf.page, this.listConf.limit, res.count);
				})
			},
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			play(id) {
				if (this.videoContext && this.videoId != id) {
					this.videoContext && this.videoContext.stop();
				}
				this.videoContext = uni.createVideoContext(id, this);
				this.videoId = id;
			},

		},
		onLoad() {
			this.imageUrl = this.globalConfig.imageUrl;
			this.getTrainList(true);
		},
		onReachBottom() {
			if (this.isMore) {
				this.page++;
				this.getTrainList(true);
				this.getNewsList(true);
				this.getVideoList(true);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fafafa;
	}

	.content {
		width: 100%;
		padding: 30rpx;

		.menu-item {
			display: flex;
			align-items: center;
			margin: 40rpx 0 30rpx 0;

			.tab-item {
				font-size: 30rpx;
				margin-right: 30rpx;
			}

			.tab-item.active {
				font-size: 34rpx;
				font-weight: bold;
				height: 60rpx;
				border-bottom: 6rpx solid #FF7626;
			}
		}

		.block {
			width: 100%;
			//margin-bottom: 50rpx;

			.title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 40rpx 0 30rpx 0;

				.t-left {
					display: flex;
					align-items: center;

					.pre-icon {
						width: 15rpx;
						height: 37rpx;
						background-color: #ff7626;
						border-radius: 16rpx;
						margin-right: 10rpx;
					}

					.name {
						font-weight: bold;
					}
				}

				.more {
					font-size: 30rpx;
					color: #999;
				}
			}

		}

		.block.swiper-block {
			height: 280rpx;
			box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
			border-radius: 20rpx;
		}

		.block.apply-block {
			.item-block {
				width: 100%;
				height: 280rpx;
				background-color: #fff;
				box-shadow: 0px 0px 4px #ccc;
				border-radius: 10rpx;
				margin: 30rpx auto 10rpx auto;
				padding: 25rpx;

				.top-title {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 1px solid #f6f6f6;
					padding-bottom: 25rpx;

					.apply-title {
						font-weight: bold;
						width: calc(100% - 20rpx);
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.btn {
						width: 160rpx;
						height: 55rpx;
						line-height: 55rpx;
						border-radius: 50rpx;
						text-align: center;
						color: #fff;
						background-color: #ff7626;
						font-size: 28rpx;
					}
				}

				.desc-block {
					display: flex;
					flex-direction: column;

					.desc {
						display: flex;
						align-items: center;
						margin: 20rpx 0 0rpx 0;

						.iconfont {
							color: #FE7526;
							margin-right: 12rpx;
							font-weight: bold;
							padding-top: 4rpx;
						}

						.desc-icon {
							width: 30rpx;
							margin-right: 12rpx;
						}

						.desc-text {
							font-size: 30rpx;

							.label {
								color: #999;
							}
						}
					}
				}
			}
		}

		.block.video-block {
			width: 100%;
			margin-top: 20rpx;

			.video-item {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.video {
					width: calc(100% / 2 - 10rpx);
					margin-bottom: 20rpx;
					display: flex;
					flex-direction: column;

					.video-icon {
						width: 100%;
						height: 220rpx;
					}

					.name {
						font-size: 30rpx;
						background-color: #fff;
						padding: 10rpx;
					}
				}

			}

			.video-item:nth-child(2n) {
				margin-right: 0rpx;
			}
		}

		.block.news-block {
			.title {
				margin: 20rpx 0 30rpx 0;
			}

			.news {
				display: flex;
				background-color: #fff;
				padding: 25rpx;
				box-shadow: 0px 0px 4px #ccc;
				border-radius: 10rpx;
				margin-bottom: 30rpx;
				height: 230rpx;

				.news-icon {
					width: 180rpx;
					height: 180rpx;
					border-radius: 16rpx;
					margin-right: 20rpx;
				}

				.news-content {
					width: calc(100% - 200rpx);
					display: flex;
					flex-direction: column;

					.news-title {
						font-weight: bold;
						margin-bottom: 10rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
						word-break: break-all;
					}

					.desc {
						color: #999;
						font-size: 28rpx;
						margin-bottom: 10rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						word-break: break-all;
						height: 80rpx;
					}

					.time {
						color: #999;
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>